package com.aaa.mbs.controller;

import com.aaa.common.util.Result;
import com.aaa.common.util.ResultStatus;
import com.aaa.mbs.entity.Member;
import com.aaa.mbs.entity.MemberVo;
import com.aaa.mbs.service.MemberService;
import com.aaa.mbs.util.SendSmUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * (Member)表控制层
 *
 * @author yuxiujie
 * @since 2021-01-21 16:09:03
 */
@RestController
@RequestMapping("/member")
@ApiOperation("member控制器")
public class MemberController {
    /**
     * 服务对象
     */
    @Resource
    private MemberService memberService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("单个对象查询")
    public Result<Member> selectOne(Integer id) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.queryById(id));
    }

    /**
     * 发送验证码
     */
    @GetMapping("/sentSM")
    public Result<String> sentSM(String phone) {
        String sentCode = SendSmUtil.sendSM(phone, SendSmUtil.randomSixCode());
        redisTemplate.opsForValue().set(phone, sentCode, 5, TimeUnit.MINUTES);
        return new Result<>(
                200, "success", sentCode);
    }

    @GetMapping("/queryMemberByParam")
    public Result<Integer> queryMemberByParam(String phoneNum) {
        return memberService.queryMemberByPhone(phoneNum);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public Result<PageInfo<Member>> queryAll(Integer pageNo, Integer pageSize, @RequestBody Member member) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.queryAll(pageNo, pageSize, member));
    }

    @GetMapping("/passwordBack")
    public Result<Boolean> passwordBack(String phoneNum, String password, String code) {
        return memberService.passwordBack(phoneNum, password, code);
    }

    /**
     * 新增数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @PostMapping("/insert")
    public Result<Member> insert(@RequestBody Member member) {
        Member insert = this.memberService.insert(member);
        if (insert != null) {
            return new Result<>(ResultStatus.SUCCESS.getCode(),
                    ResultStatus.SUCCESS.getMessage(),
                    insert);
        }
        return new Result<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMessage(), null);
    }

    @GetMapping("/insertMember")
    public Result<Boolean> insertMember(String phoneNum, String password, String code) {
        MemberVo member = new MemberVo();
        member.setPhoneNum(phoneNum);
        member.setPassword(password);
        member.setCode(code);
        return memberService.insertMember(member);
    }

    /**
     * 修改数据
     *
     * @param member 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Result<Member> update(@RequestBody Member member) {
        String idCard = member.getIdCard();
        String strSex = idCard.substring(16, 17);
        int sex = Integer.parseInt(strSex);
        if(sex%2==0){
            member.setSex("0");
        }else {
            member.setSex("1");
        }
        Member update = memberService.update(member);
        if (update != null) {
            return new Result<>(ResultStatus.SUCCESS.getCode(),
                    ResultStatus.SUCCESS.getMessage(),
                    update);
        }
        return new Result<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMessage(), null);
    }

    /**
     * 通过主键删除数据
     *
     * @param memberId 主键
     * @return 是否成功
     */
    @DeleteMapping("deleteById")
    public Result<Boolean> deleteById(Integer memberId) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.deleteById(memberId));
    }

    /**
     * 功能描述:根据用户名获取用户信息
     *
     * @param userName String
     * @return com.aaa.common.util.Result
     */
    @GetMapping("/getMemberByUserName/{userName}")
    public Result<Member> getMemberByUserName(@PathVariable("userName") String userName) {
        return new Result<>(ResultStatus.SUCCESS.getCode(),
                ResultStatus.SUCCESS.getMessage(),
                this.memberService.getMemberByUserName(userName));
    }

    @PutMapping("/updateFront")
    public Result<Member> updateFront(@RequestBody Member member) {
        return update(member);
    }
    
    @GetMapping("/changePassword/{memberId}/{oldPassword}/{newPassword}")
    public Result<Boolean> changePassword(@PathVariable("memberId") Integer memberId,
                                          @PathVariable("oldPassword") String oldPassword,
                                          @PathVariable("newPassword") String newPassword) {
        return memberService.updatePassword(memberId, oldPassword, newPassword);
    }

    @GetMapping("/updateAccountAndIntegration/{memberId}/{totalPrice}")
    public double updateAccountAndIntegration(@PathVariable("memberId") int memberId, @PathVariable("totalPrice") double totalPrice){
        return this.memberService.updateAccountAndIntegration(memberId, totalPrice);
    }
}